local blaze = require 'blaze'
local log = blaze.logging.get("create-consts")

return function()
    local is_inited = false

    local M = setmetatable({}, { __index = function(t, key)
        if is_inited then
            log.warn(string.format("常量字段不存在：%s", key))
            return
        end

        local blaze = require "blaze"
        local app = require "app"
        local log = blaze.logging.get("consts")
        local game = app.game.running
        local ok, result

        -- 先用所有依赖库的consts填充
        for _, def in pairs(game.libs) do
            ok, result = pcall(require, def .. ".util.consts")
            if ok then
                for k, v in pairs(result) do
                    rawset(t, k, v)
                end
            end
        end

        -- 再用游戏本身的consts覆盖
        ok, result = pcall(require, game.package .. ".util.consts")
        if ok then
            for k, v in pairs(result) do
                rawset(t, k, v)
            end
        end

        is_inited = true

        local ret = rawget(t, key)
        if ret == nil then
            log.warn(string.format("没有找到常量字段：%s", key))
        end
        return ret
    end})
    return M
end